﻿using System;
using System.Collections.Generic;
using System.Data;
using CoreSoft.Frame.Entity;
using CoreSoft.Frame.Entity.http;
using FERROU8API.Pub;
using U8Login;
using UFIDA.U8.MomServiceCommon;
using UFSoft.U8.Framework.ContextSrv;
using UFSoft.U8.Framework.DBConnection;
using System.Text;

namespace FERROU8API.Plugin
{
    /// <summary>
    /// 销售出库单 
    /// </summary>
    public class Rd32Plugin
    {                                                                                              
        public static string constr = "";
        private string url = "";

         
       
        public static string getSyncDbConnection(string server , string user, string psd, string db)
        {
            return "Server = " + server + ";User = " + user + "; Pwd = " + psd + ";Database = " + db;
        }

        public bool Delete_After(ref MSXML2.IXMLDOMDocument2 domhead, ref MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            try
            {
                MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

                //从上下文获取U8Login对象
                U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
                if (login != null)
                    constr = login.UFDataConnstringForNet;
                if (string.IsNullOrEmpty(constr))
                {
                    errMsg = "Rd32Plugin，Delete_After，获取连接串失败";
                    DebugWriteLog.WriteLine("Rd09Plugin", "Delete_After", errMsg);
                    return false;
                }

                string accid = login.get_cAcc_Id();
                MSXML2.IXMLDOMNode tmphead = domhead.selectSingleNode("//z:row");

                int id = tmphead.attributes.getNamedItem("id").nodeValue.ObjToInt32();//出库单ID

                Dictionary<string, string> js;
                js = GetCustDic(accid, id, 1);
                if (js == null)
                {
                    return true;
                }

                var result = HttpHelper.PostDataToServerDic(url, js, "application/x-www-form-urlencoded");
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    DebugWriteLog.WriteLine("Rd32Plugin", "Update_After", errMsg);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                DebugWriteLog.WriteLine("Rd32Plugin", "Delete_After", ex);
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }


        public bool Update_After(ref MSXML2.IXMLDOMDocument2 domhead, ref MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            try
            {
                MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

                //从上下文获取U8Login对象
                U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
                if (login != null)
                    constr = login.UFDataConnstringForNet;
                if (string.IsNullOrEmpty(constr))
                {
                    errMsg = "Rd32Plugin，Update_After，获取连接串失败";
                    DebugWriteLog.WriteLine("Rd32Plugin", "Update_After", errMsg);
                    return false;
                }

                string accid = login.get_cAcc_Id();
                MSXML2.IXMLDOMNode tmphead = domhead.selectSingleNode("//z:row");
                                                                                                              
                int id = tmphead.attributes.getNamedItem("id").nodeValue.ObjToInt32();//出库单ID
                                                                 
                Dictionary<string, string> js;
                js = GetCustDic(accid, id, 0);
                if (js == null)
                {
                    return true;
                }

                var result = HttpHelper.PostDataToServerDic(url, js, "application/x-www-form-urlencoded");
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    DebugWriteLog.WriteLine("Rd32Plugin", "Update_After", errMsg);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                DebugWriteLog.WriteLine("Rd32Plugin", "Update_After", ex);
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }



        private Dictionary<string, string> GetCustDic(string accid, int id, int operation)
        {
            DebugWriteLog.WriteLine("销售出库单插件", "ccode", "id=" + id);

            try
            {

                UFIDA.U8.DB.Model.rdrecord32 model = UFIDA.U8.DB.DAL.rdrecord32.GetModel(id.ObjToInt32(), constr);
                if (model == null)
                {
                    DebugWriteLog.WriteLine("销售出库单插件", "table", " Nopermission");
                    return null;
                }
                if (!string.IsNullOrEmpty(model.cDefine8) && model.cDefine8.Equals("是"))
                {
                    if (string.IsNullOrEmpty(model.cDefine10))
                    {
                        throw new Exception("内部协同订单，最终客户不能为空");
                    }
                    UFIDA.U8.DB.Model.Customer cus = UFIDA.U8.DB.DAL.Customer.GetModel(model.cCusCode, constr);
                    if (cus == null || string.IsNullOrEmpty(cus.cCusDefine1))
                    {
                        throw new Exception("销售出库单插件, 客户档案设置有误，未设置对应orgcode");
                    }

                    url = cus.cCusDefine7;
                    if (string.IsNullOrEmpty(url))
                    {
                        throw new Exception("销售出库单插件, 客户档案设置有误,未配置接口地址");
                    }
                    url = cus.cCusDefine7 + "Sync_RD32/Create?";

                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    dic.Add("orgcode", cus.cCusDefine1);
                    dic.Add("operation", operation.ToString());
                    dic.Add("primarykey", model.cCode);
                    dic.Add("csource", accid);
                    dic.Add("data", model.ToJson());


                    DebugWriteLog.WriteLine("销售出库单插件", "抛出参数", model.ToJson());
                    return dic;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception e)
            {
                DebugWriteLog.WriteLine("销售出库单插件", "异常", e);
                throw e;
            }
        }             

    }
}